package com.hbzhit.modules.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hbzhit.common.annotation.SysLog;
import com.hbzhit.common.utils.PageUtils;
import com.hbzhit.common.utils.R;
import com.hbzhit.common.validator.ValidatorUtils;
import com.hbzhit.modules.sys.entity.SysConfigEntity;
import com.hbzhit.modules.sys.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 系统配置信息
 *
 * @author Mark sunlightcs@gmail.com
 */
@Api(tags = {"系统参数配置"})
@RestController
@RequestMapping("/sys/config")
public class SysConfigController {
	@Autowired
	private SysConfigService sysConfigService;

	@ApiOperation(value = "分页列表")
	@GetMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = sysConfigService.queryPage(params);

		return R.ok(page);
	}

    @ApiOperation(value = "根据ID获取详情")
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") Long id){
		SysConfigEntity config = sysConfigService.getById(id);

		return R.ok(config);
	}

	@ApiOperation(value = "根据KEY获取详情")
	@GetMapping("/info/key/{key}")
	public R info(@PathVariable("key") String key){
		SysConfigEntity config = sysConfigService.getOne(new LambdaQueryWrapper<SysConfigEntity>()
				.eq(SysConfigEntity::getParamKey, key)
		, false);

		return R.ok(config);
	}

	@ApiOperation(value = "保存")
	@SysLog("保存配置")
	@PostMapping("/save")
	@RequiresPermissions("sys:config:save")
	public R save(@RequestBody SysConfigEntity config){
		ValidatorUtils.validateEntity(config);

		sysConfigService.saveConfig(config);

		return R.ok();
	}

	@ApiOperation(value = "修改")
	@SysLog("修改配置")
	@PostMapping("/update")
	@RequiresPermissions("sys:config:update")
	public R update(@RequestBody SysConfigEntity config){
		ValidatorUtils.validateEntity(config);

		sysConfigService.update(config);

		return R.ok();
	}

	@ApiOperation(value = "删除")
	@SysLog("删除配置")
	@PostMapping("/delete")
	@RequiresPermissions("sys:config:delete")
	public R delete(@RequestBody Long[] ids){
		sysConfigService.deleteBatch(ids);

		return R.ok();
	}

}
